// SettingTableDlg.cpp : implementation file
//

#include "stdafx.h"
#include "backManage.h"
#include "SettingTableDlg.h"
#include "backManageDlg.h"


// CSettingTableDlg dialog

IMPLEMENT_DYNAMIC(CSettingTableDlg, CMaxDisplayDlg)

CSettingTableDlg::CSettingTableDlg(CWnd* pParent /*=NULL*/)
	: CMaxDisplayDlg(CSettingTableDlg::IDD, pParent)
{

}


CSettingTableDlg::~CSettingTableDlg()
{
}

void CSettingTableDlg::DoDataExchange(CDataExchange* pDX)
{
	CMaxDisplayDlg::DoDataExchange(pDX);
	DDX_Text(pDX, IDC_EDIT_SERPORT, m_nServerPort);
	DDX_Control(pDX, IDC_IPADDRESS_SERIP, m_nIP);
	DDX_Text(pDX, IDC_EDIT_NETSTATUS, m_nNetStatus);
	DDX_Text(pDX, IDC_EDIT_UDPPORT, m_nUDPPort);
}


BEGIN_MESSAGE_MAP(CSettingTableDlg, CMaxDisplayDlg)
	ON_BN_CLICKED(IDC_BTN_SAVE, &CSettingTableDlg::OnBnClickedBtnSave)
	ON_BN_CLICKED(IDC_BTN_CONNECT, &CSettingTableDlg::OnBnClickedBtnConnect)
	ON_BN_CLICKED(IDC_BTN_OK, &CSettingTableDlg::OnBnClickedBtnOk)
	ON_BN_CLICKED(IDC_BTN_CANCEL, &CSettingTableDlg::OnBnClickedBtnCancel)
END_MESSAGE_MAP()


// CSettingTableDlg message handlers
BOOL CSettingTableDlg::OnInitDialog()
{
	CMaxDisplayDlg::OnInitDialog();

	 char sText[200]= {0};
	 WCharToMByte(m_nServerIP, sText,sizeof(sText)/sizeof(sText[0]));

	DWORD   dwIP;   
	dwIP   =   inet_addr(sText);   
	unsigned   char   *pIP   =   (unsigned   char*)&dwIP;   
	m_nIP.SetAddress(*pIP,   *(pIP+1),   *(pIP+2),   *(pIP+3));
	if(m_nNetStatus=="Connnected OK")
	{
		SetDlgItemText(IDC_BTN_CONNECT, _T("Disconnect"));
	}
	else
	{
		SetDlgItemText(IDC_BTN_CONNECT, _T("Connect"));
	}

	((CButton*)GetDlgItem( IDC_BTN_CONNECT ))->EnableWindow(TRUE);  

	UpdateData(false);
	return TRUE; 

}

void CSettingTableDlg::OnBnClickedBtnSave()
{
	// TODO: Add your control notification handler code here
	UpdateData(true);
	DWORD   dwIP;   
	u8   *pIP;   

	m_nIP.GetAddress(dwIP);   
	pIP=(u8*)&dwIP;   
	m_nServerIP.Format(_T("%u.%u.%u.%u"),*(pIP+3),   *(pIP+2),   *(pIP+1),   *pIP);
	SaveBMConfigXML(&m_nServerIP, &m_nServerPort, &m_nUDPPort);
}

void CSettingTableDlg::OnBnClickedBtnConnect()
{
	// TODO: Add your control notification handler code here
	UpdateData(true);
	BOOL ret;
	if(m_nNetStatus=="Connnected OK")
	{
		TCPServerClose();
		m_nNetStatus="Not Connnected";
		SetDlgItemText(IDC_BTN_CONNECT, _T("Connect"));

	}
	else
	{
		ret=TCPServerConncet(m_nServerIP, m_nServerPort);
		m_nNetStatus=ret?"Connnected OK":"Not Connnected";
		SetDlgItemText(IDC_BTN_CONNECT, _T("Disconnect"));
	}
	UpdateData(false);	
}

void CSettingTableDlg::OnBnClickedBtnOk()
{
	// TODO: Add your control notification handler code here
	if((UpdateData( )==FALSE)) return;
	DWORD   dwIP;   
	u8   *pIP;   

	m_nIP.GetAddress(dwIP);   
	pIP=(u8*)&dwIP;   
	m_nServerIP.Format(_T("%u.%u.%u.%u"),*(pIP+3),   *(pIP+2),   *(pIP+1),   *pIP);
	SaveBMConfigXML(&m_nServerIP, &m_nServerPort, &m_nUDPPort);
	OnOK();
}

void CSettingTableDlg::OnBnClickedBtnCancel()
{
	// TODO: Add your control notification handler code here
	OnCancel();
}

